OTAlloc
Allocates an XTI data structure.C INTERFACE
void* OTAlloc (EndpointRef ref, OTStructType structType, UInt32 fields, OSStatus* err);C++ INTERFACE
void* TEndpoint::Alloc(OTStructType structType, int fields, OSStatus* err = NULL);PARAMETERS
ref
- The endpoint reference of the endpoint for which the data structure is allocated.
structType
- A long specifying the constant name of the structure for which memory is to be allocated. Possible values for the
structType
parameter are given by the structure types enumeration (page 3-47).fields
- An integer specifying the structure fields for which buffers are to be allocated.
- Each structure that you can specify for
structType
, except forT_INFO
, contains at least one field of typeTNetbuf
. For each such field, you can use thefields
parameter to specify that the buffer described byTNetbuf
also be allocated. The length of the allocated buffer is at least as large as the size returned for the endpoint by theOTGetEndpointInfo
function. For each buffer allocated, theOTAlloc
function sets themaxlen
field to the length of the buffer and sets thelen
field to 0.- You can specify one or more constant names for the
fields
parameter. Possible values for constant names are given by the buffer types enumeration (page 3-42). To specify more than one constant name, use thebitOR
operator to combine values.DESCRIPTION
TheOTAlloc
function allocates a data structure for use in a subsequent call. You use thestructType
parameter to specify the structure to be allocated and thefields
parameter to specify the substructures to be allocated. If theOTAlloc
function succeeds, it returns a pointer to the desired structure. TheOTAlloc
function is provided mainly for compatibility with XTI. Although using this function along with theOTFree
function can save you coding work, this is at the price of slower performance. In general, you should not allocate and free structures on every call. Instead, you should declare structures that are to be passed as parameters to endpoint functions just as you would any other variables or data structures.It is easiest to understand what the
OTAlloc
function does by considering what you would have to do if you did not use it. If you declaredstructType
structures as normal data structures, you would have to declare the data structure and then initialize themaxlen
andbuf
fields of everyTNetbuf
type field contained by the structure. To determine the appropriate size of each buffer, you would have to call theOTGetEndpointInfo
function. For example, if you call theOTGetProtAddress
function to get the protocol address of an endpoint, you must pass a parameter of typeTBind
. Theaddr.buf
field of theTBind
structure points to a buffer that is large enough to hold the endpoint's protocol address. To determine how large the buffer has to be, you call theOTGetEndpointInfo
function; then you allocate the memory for the buffer and initialize theaddr.buf
field to point to the buffer and initialize theaddr.maxlen
field to specify how large the buffer can be. TheOTAlloc
function does all this work for you. Given the previous example, if you make the call
TBind* boundAddr = OTAlloc(T_BIND, T_ADDR);theOTAlloc
function allocates theTBind
structure, initializes theTNetbuf
field that is used to describe the endpoint address, and allocates memory for the buffer in which the address is to be stored. All buffers allocated are guaranteed to be of the appropriate size for the kind of endpoint specified by theref
parameter. You must not use the pointer returned by theOTAlloc
function in calls to any other endpoint.If the requested structure contains
TNetbuf
fields and you do not specify these fields using thefields
parameter, theOTAlloc
function sets themaxlen
,len
, andbuf
fields of theseTNetbuf
structures to 0.SPECIAL CONSIDERATIONS
If you specifyT_UDATA
orT_ALL
for thefields
parameter and the endpoint information structure defines thetsdu
oretsdu
size for the endpoint to be of infinite length, theOTAlloc
function does not allocate a data buffer for the endpoint.VALID STATES
AllSEE ALSO
To deallocate memory allocated with theOTAlloc
function, use theOTFree
function (described next).You use the structure types enumeration (page 3-47) to specify the structure for which memory is to be allocated.
You use the buffer types enumeration (page 3-42) to specify which
TNetbuf
structures should be allocated for the structure type you select.The
TBind
structure (page 3-51) specifies the address of an endpoint.The
TEndpointInfo
structure (page 3-48) specifies the maximum size of buffers used to hold an endpoint's address, options, and data.To allocate raw memory, use the
OTAllocMem
function, and to deallocate the allocated raw memory, use theOTFreeMem
function, both described in the chapter "Process Management."